package com.sofoscorp.android.gestcobranzamobile;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class KNA1_BO {

	private KNA1_Str data;
	private KNA1_Str[] datas;
	private String path_FromSAP = "data/data/com.sofoscorp.android.initdb/databases/FromSAP";
	private String table_name = "KNA1";
	private SQLiteDatabase db1;
	private String[] campos = new String[] {"KUNNR", "NAME1", "NAME2", "DIRECCION",
											"TLF", "EMAIL", "STCD1", "STCD2", "KDKG1",
											"KVGR1", "VKORG", "VTWEG", "SPART", "ZTERM",
											"KDGRP", "VKGRP", "BZIRK", "ERDAT", "PLTYP",
											"KTOKD", "AUFSD"};
	
	public boolean do_conn() {
		try{
			db1 = SQLiteDatabase.openDatabase(path_FromSAP, null, SQLiteDatabase.OPEN_READWRITE);
		}catch(SQLiteException e){
			db1 = null;
		}
		
		if(db1 != null){
			return true;
		}else{
			return false;
		}
		
	}
	
	
	public KNA1_Str get_single_kunnr(String kunnr){
		
		Cursor c = null;
		String where = "KUNNR = ?";
		String[] whereArgs;       

		whereArgs = new String[]{kunnr};
		
		if(kunnr.equals("") || kunnr.equals(null)){
			data=null;
			return data;
		}
		
		if(db1 != null){
			c = db1.query(table_name, campos, where, whereArgs, null, null, null);
		}
		
		if (c.moveToFirst()){
			
			data = new KNA1_Str(c.getString(0), c.getString(1), c.getString(2),
					            c.getString(3), c.getString(4), c.getString(5),
					            c.getString(6), c.getString(7), c.getString(8),
					            c.getString(9), c.getString(10), c.getString(11),
					            c.getString(12), c.getString(13), c.getString(14),
					            c.getString(15), c.getString(16), c.getString(17),
					            c.getString(18), c.getString(19), c.getString(20));
			
		}else{
			data = null;
		}		
		
		db1.close();
		return data;
	}
	
	public KNA1_Str[] get_all_kunnr(){
		
		Cursor c = null; 
		ArrayList<KNA1_Str> data_result = new ArrayList<KNA1_Str>();

		if(db1 != null){
			c = db1.query(table_name, campos, null, null, null, null, null);
		}
		
		if (c.moveToFirst()){
			
			do{
				
				data_result.add(new KNA1_Str(c.getString(0), c.getString(1), c.getString(2),
			                                 c.getString(3), c.getString(4), c.getString(5),
			                                 c.getString(6), c.getString(7), c.getString(8),
			                                 c.getString(9), c.getString(10), c.getString(11),
			                                 c.getString(12), c.getString(13), c.getString(14),
			                                 c.getString(15), c.getString(16), c.getString(17),
			                                 c.getString(18), c.getString(19), c.getString(20)));	
				
			}while(c.moveToNext());
			
		}else{
			data_result = null;
			datas = null;
			db1.close();
			return datas;
		}		
		
		//for (int i=0; i<=data_result.size(); i++){
		//	datas[i] = (KNA1_Str)data_result.get(i).toString();
		//}
		
		datas = data_result.toArray(new KNA1_Str[0]);
		db1.close();
		return datas;
	}
	
	public KNA1_Str[] get_all_kunnr(KNB1_Str itKNB1[]){
		ArrayList<KNA1_Str> data_result = new ArrayList<KNA1_Str>();
		Cursor c;
		String where;
		String[] whereArgs;
		for (int i = 0; i < itKNB1.length; i++) {
			c = null;
			where = null;
			whereArgs = null;
			
			where = "KUNNR = ?";       
			whereArgs = new String[]{itKNB1[i].getKUNNR()};
			
			if(db1 != null){
				c = db1.query(table_name, campos, where, whereArgs, null, null, null);
			}
			
			if (c.moveToFirst()){
				
				data_result.add(new KNA1_Str(	c.getString(0), c.getString(1), c.getString(2),
						            			c.getString(3), c.getString(4), c.getString(5),
						            			c.getString(6), c.getString(7), c.getString(8),
						            			c.getString(9), c.getString(10), c.getString(11),
						            			c.getString(12), c.getString(13), c.getString(14),
						            			c.getString(15), c.getString(16), c.getString(17),
						            			c.getString(18), c.getString(19), c.getString(20)));
			}else{
				datas = null;
			}		
		}
		datas = data_result.toArray(new KNA1_Str[0]);
		db1.close();
		return datas;
	}
	
}
